package cn.edu.neusoft.amicooldemo.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.edu.neusoft.amicooldemo.R;
import cn.edu.neusoft.amicooldemo.ViewtcaseActivity;
import cn.edu.neusoft.amicooldemo.adapter.TcaseAdapter;
import cn.edu.neusoft.amicooldemo.bean.TcaseBean;
import cn.edu.neusoft.amicooldemo.factory.AmicoolFactory;
import cn.edu.neusoft.amicooldemo.iface.AmicoolListener;
import cn.edu.neusoft.amicooldemo.iface.OnRecyclerViewItemClickListener;

public class TcaseFragment extends Fragment{
	private RecyclerView recyclerView;

	private List<TcaseBean> list;
	protected TcaseAdapter adapter;
	private static int cur_page=1;private int lastVisibleItemPosition;
	private static String mod;
	private String SessionID;
	private SharedPreferences sp;
	private AmicoolListener<TcaseBean> listener;
	public TcaseFragment() {

		mod = "tcase";
		list = new ArrayList<TcaseBean>();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		//T.showShort(getActivity(), "TcaseFragment==onCreateView");
		View view = inflater.inflate(R.layout.fragment_tcase, container, false);
		sp = getActivity().getSharedPreferences("amicool_user", Context.MODE_PRIVATE);
		SessionID=sp.getString("SessionID","dfrcg3211kjb1vbe3ibgftkfa6");
		final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
		recyclerView = (RecyclerView) view.findViewById(R.id.tab_tcase_list);
		recyclerView.setLayoutManager(layoutManager);
		recyclerView.setItemAnimator(new DefaultItemAnimator());
		adapter = new TcaseAdapter(getActivity(),list);
		adapter.setOnItemClickListener(new TcaseAdapter.OnItemClickListener(){

			@Override
			public void onItemClick(View view, int position) {

				Intent intent=new Intent(getActivity(), ViewtcaseActivity.class);
				intent.putExtra("resid",list.get(position).getId());
				intent.putExtra("userid",list.get(position).getUserid());
				startActivity(intent);
			}

			@Override
			public void onItemLongClick(View view, int position) {

			}
		});

		recyclerView.setAdapter(adapter);
		listener = new AmicoolListener<TcaseBean>() {
			@Override
			public void onResponse(List<TcaseBean> l) {
				list.removeAll(l);
				list.addAll(0,l);
				adapter.setList(list);

			}

			@Override
			public void loadNextData(List<TcaseBean> l) {
				list.addAll(l);
				adapter.notifyDataSetChanged();
			}

			@Override
			public void onFail(String msg) {
				Toast.makeText(getActivity(),"fail:"+msg,Toast.LENGTH_SHORT).show();
			}

			@Override
			public void loadPreData(List<TcaseBean> l) {

			}


		};
		recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
				super.onScrollStateChanged(recyclerView, newState);
				if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == adapter.getItemCount()) {
					cur_page+=1;
					AmicoolFactory.getInstance().getResultList(mod,cur_page,SessionID,listener);

				}


			}

			@Override
			public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
				super.onScrolled(recyclerView, dx, dy);

				lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();;

			}
		});
		if(cur_page==1)
		AmicoolFactory.getInstance().getResultList(mod,cur_page,SessionID, listener);
		return view;
	}

	
}
